package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.web.controller.BaseController;

import org.hibernate.engine.spi.EntityEntryExtraState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tech.yunland.shop.domain.model.ChargeBill;
import tech.yunland.shop.domain.model.SaleBill;
import tech.yunland.shop.domain.model.SaleBillEntry;
import tech.yunland.shop.domain.model.report.Commission;
import tech.yunland.shop.domain.model.report.Income;
import tech.yunland.shop.domain.service.report.IncomeService;
import tech.yunland.shop.domain.until.ChangeSerizliableResult;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 收入控制器
 *
 * @author 王睿智<geekrize@gmail.com>
 */

@RestController
@RequestMapping("/Income")
public class IncomeController extends BaseController {

	@Autowired
	private IncomeService incomeService;

	/**
	 * 店铺月报
	 *
	 * @param bizDate
	 * @return Map<String, Object>
	 * @throws ParseException
	 */
	@RequestMapping(value = "/month", method = RequestMethod.GET)
	public Map<String, Object> getMonth(@RequestParam Date bizDate) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String StringDate = sdf.format(bizDate);

		Date date = sdf.parse(StringDate);

		return incomeService.getMonth(date, primaryOrganId);
	}

	/**
	 * 店铺销售总额
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public Map<String, Object> getAll() throws ParseException {
		return incomeService.getAll(primaryOrganId);
	}

	/**
	 * 店铺日报
	 *
	 * @param organId
	 * @param startIndex
	 * @param length
	 * @return Income
	 * @throws ParseException
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public Pagination<Income> getResultInfo(@RequestParam(required = false) String bizDate,
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows)
			throws ParseException {

		Pagination<Income> result = new Pagination<Income>(page, rows);
		int startIdx = result.getStartIdx();

		int count = incomeService.getResultSize(bizDate, this.primaryOrganId);
		List<Income> records = incomeService.getResultInfo(bizDate, this.primaryOrganId, startIdx, rows);

		result.setCount(count);
		result.setRecords(records);

		return result;
	}

	/**
	 * 现金/消费收入详情
	 * 
	 * @param bizDate
	 * @param dateType
	 *            年/月/日(year/month/day)
	 * @param page
	 * @param rows
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/incomeCashEntry", method = RequestMethod.GET)
	public Pagination<SaleBill> getIncomeEntry(@RequestParam(required = false) String bizDate,
			@RequestParam(required = false) String type, // 现金/消费(cash/deduction)
			@RequestParam(required = false) String dateType, // 年/月/日(year/month/day)
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows)
			throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat();

		Pagination<SaleBill> result = new Pagination<SaleBill>(page, rows);
		int startIdx = result.getStartIdx();

		List<SaleBill> records = new ArrayList<>();

		if (type.equals("cash")) {
			records = incomeService.getIncomeEntry("cash", dateType, bizDate, primaryOrganId);
		} else if (type.equals("deduction")) {
			records = incomeService.getIncomeEntry("deduction", dateType, bizDate, primaryOrganId);
		}
		result.setCount(records.size());
		result.setRecords(records);

		return result;
	}

	/**
	 * 充值收入详情
	 * 
	 * @param bizDate
	 * @param dateType
	 * @param page
	 * @param rows
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/incomeChargeEntry", method = RequestMethod.GET)
	public Pagination<ChargeBill> getIncomeChargeEntry(@RequestParam(required = false) String bizDate,
			@RequestParam(required = false) String dateType, // 年/月/日(year/month/day)
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows)
			throws ParseException {

		// SimpleDateFormat sdf = new SimpleDateFormat();
		//
		// if (dateType.equals("year")) {
		// sdf = new SimpleDateFormat("yyyy");
		// } else if (dateType.equals("month")) {
		// sdf = new SimpleDateFormat("yyyy-MM");
		// } else if (dateType.equals("day")) {
		// sdf = new SimpleDateFormat("yyyy-MM-dd");
		// }
		// String StringDate = sdf.format(bizDate);
		// Date date = sdf.parse(StringDate);

		Pagination<ChargeBill> result = new Pagination<ChargeBill>(page, rows);
		int startIdx = result.getStartIdx();

		List<ChargeBill> records = new ArrayList<>();
		records = incomeService.getChargeEntry(dateType, bizDate, primaryOrganId);

		result.setCount(records.size());
		result.setRecords(records);
		return result;
	}

	/**
	 * 桌面端店铺日报(扣费/充值/现金)详情
	 * 
	 * @param bizDate
	 * @param type
	 * @param dateType
	 * @param page
	 * @param rows
	 * @return
	 * @throws List<Map<String,
	 *             Object>>
	 */
	@RequestMapping(value = "/incomeEntry/desktop", method = RequestMethod.GET)
	public Pagination<HashMap<String, Object>> getIncomeDesktopEntry(@RequestParam(required = false) String bizDate,
			@RequestParam(required = false) String type, // 现金/扣费/充值(cash/deduction/charge)
			@RequestParam(required = false) String dateType, // 年/月/日(year/month/day)
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows)
			throws ParseException {

		Pagination<HashMap<String, Object>> result = new Pagination(page, rows);

		List<Map<String, Object>> returnEntry = new ArrayList<>();
		int startIdx = result.getStartIdx();
		int count = 0;
		if (type.equals("cash")) {// 按店铺查询剩余总额
			returnEntry = incomeService.getIncomeDesktopEntry(type, dateType, bizDate, primaryOrganId, startIdx, rows,"details");
			count=incomeService.getIncomeDesktopEntry(type, dateType, bizDate, primaryOrganId, startIdx, rows,"size").size();
		} else if (type.equals("deduction")) {// 按会员查询剩余总额
			returnEntry = incomeService.getIncomeDesktopEntry(type, dateType, bizDate, primaryOrganId, startIdx, rows,"details");
			count=incomeService.getIncomeDesktopEntry(type, dateType, bizDate, primaryOrganId, startIdx, rows,"size").size();
		} else if (type.equals("charge")) {// 按会员查询明细
			returnEntry = incomeService.getIncomeDesktopEntry(type, dateType, bizDate, primaryOrganId, startIdx, rows,"details");
			count=incomeService.getIncomeDesktopEntry(type, dateType, bizDate, primaryOrganId, startIdx, rows,"size").size();
		}
		List<HashMap<String,Object>> entyrs = ChangeSerizliableResult.changeToSerizliable(returnEntry);
		result.setRecords(entyrs);
		result.setCount(count);

		return result;
	}

}
